package eln.zaislu.muziejus;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.view.SurfaceView;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.util.FloatMath;
import android.util.Log;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;


public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {
	 
	
	 public static MainThread thread;
	 static List<Actor> imo=new ArrayList<Actor>();
	 private SimpleActor fonas;
	 private Actor imobj=null;
	 private static final String TAG = MainThread.class.getSimpleName();
	 private static boolean menuIsVisible=false;
	 public static List<ObjectHelper> objects=new ArrayList<ObjectHelper>();
	 static boolean deleteStatus=false;
	 
	//These matrices will be used to move and zoom image
	 Matrix matrix = new Matrix();
	 Matrix savedMatrix = new Matrix();  

	 // We can be in one of these 3 states
	 static final int NONE = 0;
	 static final int DRAG = 1;
	 static final int ZOOM = 2;
	 static final int DRAW =3;
	 int mode = NONE;

	 // Remember some things for zooming
	 PointF start = new PointF();
	 PointF mid = new PointF();
	 float oldDist = 1f;

	 // Limit zoomable/pannable image
	 private float[] matrixValues = new float[9];
	 private float maxZoom;
	 private float minZoom;
	 private float height;
	 private float width;
	 private float initheight;
	 private float initwidth;
	 private Bitmap initbitmap;
	 private RectF viewRect;
	 float scale =1.0f;
	 Context ctx;
	 private long lastTouchTime = -1;
	
	 public MainGamePanel(Context context) {
	  super(context);
	  ctx=context;
	  // adding the callback (this) to the surface holder to intercept events
	  getHolder().addCallback(this);
	  
	  
	/*  ObjectHelper ob=new ObjectHelper(R.drawable.dots,false,false,R.drawable.dots);
	  CollisionObj cob = new CollisionObj();
	  cob.collisionobjid=1;
	  cob.animationid=3;
	  ob.co.add(cob);
	  objects.add(ob);
	  objects.add(new ObjectHelper(R.drawable.dots,true,false,R.drawable.walk_elaine));
	  objects.add(new ObjectHelper(R.drawable.dots,false,false,R.drawable.dots));
	  objects.add(new ObjectHelper(R.drawable.dots,true,true,R.drawable.walk_elaine));*/
	  
	  /*0*/objects.add(new ObjectHelper(R.drawable.fonas_viduramziai,false,false,R.drawable.fonas_viduramziai,R.raw.aplinkos,0));
	  /*1*/objects.add(new ObjectHelper(R.drawable.fonas_dvaras_kaimas,false,false,R.drawable.fonas_dvaras_kaimas,R.raw.aplinkos,0));
	  /*2*/objects.add(new ObjectHelper(R.drawable.fonas_modernus,false,false,R.drawable.fonas_modernus,R.raw.aplinkos,0));
	  
	  //objects.add(new ObjectHelper(R.drawable.berniukas_riteris_icon,false,false,R.drawable.berniukas_riteris,R.raw.flamemagic));
	  /*3*/ObjectHelper ob10=new ObjectHelper(R.drawable.berniukas_riteris_be_icon,false,false,R.drawable.berniukas_riteris_be,R.raw.veikejai,0);
	  CollisionObj cob210 = new CollisionObj();
	  cob210.collisionobjid=32;
	  cob210.animationid=103;
	  ob10.co.add(cob210);
	  CollisionObj cob10 = new CollisionObj();
	  cob10.collisionobjid=35;
	  cob10.animationid=93;
	  ob10.co.add(cob10);
	  objects.add(ob10);
	  //objects.add(new ObjectHelper(R.drawable.berniukas_svecias_icon,false,false,R.drawable.berniukas_svecias,R.raw.flamemagic));
	  /*4*/ObjectHelper ob9=new ObjectHelper(R.drawable.berniukas_svecias_icon,false,false,R.drawable.berniukas_svecias,R.raw.veikejai,0);
	  CollisionObj cob9 = new CollisionObj();
	  cob9.collisionobjid=29;
	  cob9.animationid=91;
	  ob9.co.add(cob9);
	  objects.add(ob9);
	  /*5*/objects.add(new ObjectHelper(R.drawable.grumotojas_icon,false,false,R.drawable.grumotojas,R.raw.veikejai,0));
	  /*6*/objects.add(new ObjectHelper(R.drawable.mergaite_icon,false,false,R.drawable.mergaite,R.raw.veikejai,0));
	  /*7*/objects.add(new ObjectHelper(R.drawable.plesikas_icon,false,false,R.drawable.plesikas,R.raw.veikejai,0));
	  //objects.add(new ObjectHelper(R.drawable.riteris_tevas_icon,false,false,R.drawable.riteris_tevas,R.raw.flamemagic));
	  /*8*/ObjectHelper ob12=new ObjectHelper(R.drawable.riteris_tevas_icon,false,false,R.drawable.riteris_tevas,R.raw.veikejai,0);
	  CollisionObj cob12 = new CollisionObj();
	  cob12.collisionobjid=37;
	  cob12.animationid=94;
	  ob12.co.add(cob12);
	  objects.add(ob12);
	  //objects.add(new ObjectHelper(R.drawable.riteris_icon,false,false,R.drawable.riteris,R.raw.flamemagic));
	  /*09*/ObjectHelper ob13=new ObjectHelper(R.drawable.riteris_icon,false,false,R.drawable.riteris,R.raw.veikejai,0);
	  CollisionObj cob13 = new CollisionObj();
	  cob13.collisionobjid=39;
	  cob13.animationid=92;
	  ob13.co.add(cob13);
	  objects.add(ob13);
	  //objects.add(new ObjectHelper(R.drawable.svecias_icon,false,false,R.drawable.svecias,R.raw.flamemagic,0));
	  /*10*/ObjectHelper ob213=new ObjectHelper(R.drawable.svecias_icon,false,false,R.drawable.svecias,R.raw.veikejai,0);
	  CollisionObj cob213 = new CollisionObj();
	  cob213.collisionobjid=39;
	  cob213.animationid=92;
	  ob213.co.add(cob213);
	  objects.add(ob213);
	  /*11*/objects.add(new ObjectHelper(R.drawable.teta_icon,false,false,R.drawable.teta,R.raw.veikejai,0));
	  /*12*/objects.add(new ObjectHelper(R.drawable.zinys_icon,false,false,R.drawable.zinys,R.raw.veikejai,0));
	  
	  //objects.add(new ObjectHelper(R.drawable.berniukas_ragatke_dvaro_icon,false,false,R.drawable.berniukas_ragatke_davro,R.raw.flamemagic));
	  /*13*/ObjectHelper ob15=new ObjectHelper(R.drawable.berniukas_dvaro_icon,false,false,R.drawable.berniukas_dv,R.raw.veikejai,0);
	 /* CollisionObj cob215 = new CollisionObj();
	  cob215.collisionobjid=50;
	  cob215.animationid=104;
	  ob15.co.add(cob215);*/
	 /* CollisionObj cob15 = new CollisionObj();
	  cob15.collisionobjid=45;
	  cob15.animationid=99;
	  ob15.co.add(cob15);*/
	  /*CollisionObj cob16 = new CollisionObj();
	  cob16.collisionobjid=47;
	  cob16.animationid=99;
	  ob15.co.add(cob16);*/
	  objects.add(ob15);
	  /*14*/objects.add(new ObjectHelper(R.drawable.grumotojas_dvaro_icon,false,false,R.drawable.grumotojas_dvaro,R.raw.veikejai,0));
	  /*15*/objects.add(new ObjectHelper(R.drawable.kaimynas_dvaro_icon,false,false,R.drawable.kaimynas_dvaro,R.raw.veikejai,0));
	  //objects.add(new ObjectHelper(R.drawable.mergaite_dvaro_icon,false,false,R.drawable.mergaite_dvaro,R.raw.flamemagic));
	  /*16*/ObjectHelper ob17=new ObjectHelper(R.drawable.mergaite_dvaro_icon,false,false,R.drawable.mergaite_dvaro,R.raw.veikejai,0);
	  CollisionObj cob17 = new CollisionObj();
	  cob17.collisionobjid=56;
	  cob17.animationid=97;
	  ob17.co.add(cob17);
	  objects.add(ob17);
	  /*17*/objects.add(new ObjectHelper(R.drawable.moteris_dvaro_icon,false,false,R.drawable.moteris_dvaro,R.raw.veikejai,0));
	  /*18*/objects.add(new ObjectHelper(R.drawable.piemuo_dvaro_icon,false,false,R.drawable.piemuo_dvaro,R.raw.veikejai,0));
	  /*19*/objects.add(new ObjectHelper(R.drawable.tevas_dvaro_icon,false,false,R.drawable.tevas_dvaro,R.raw.veikejai,0));
	  /*20*/objects.add(new ObjectHelper(R.drawable.vaikas_dvaro_icon,false,false,R.drawable.vaikas_dvaro,R.raw.veikejai,0));
	  
	  //objects.add(new ObjectHelper(R.drawable.modernus_sunus_be_icon,false,false,R.drawable.modernus_sunus_be,R.raw.veikejai,0));
	  /*21*/ObjectHelper ob418=new ObjectHelper(R.drawable.modernus_sunus_be_icon,false,false,R.drawable.modernus_sunus_be,R.raw.veikejai,0);
	  CollisionObj cob418 = new CollisionObj();
	  cob418.collisionobjid=59;
	  cob418.animationid=105;
	  ob418.co.add(cob418);
	  objects.add(ob418);
	  /*22*/objects.add(new ObjectHelper(R.drawable.grumotojas_mod_icon,false,false,R.drawable.grumotojas1,R.raw.veikejai,0));
	  /*23*/objects.add(new ObjectHelper(R.drawable.jedajus,true,false,R.drawable.anim_modernus_dzedajus_400,3600,400,1,9,R.raw.dzedajus,0));
	  /*24*/objects.add(new ObjectHelper(R.drawable.kaimynas_mod_icon,false,false,R.drawable.kaimynas,R.raw.veikejai,0));
	  /*25*/objects.add(new ObjectHelper(R.drawable.kiemsargis_mod_icon,false,false,R.drawable.kiemsargis,R.raw.veikejai,0));
	  //objects.add(new ObjectHelper(R.drawable.mergaite_mod_icon,false,false,R.drawable.mergaite1,R.raw.flamemagic));
	  /*26*/ObjectHelper ob18=new ObjectHelper(R.drawable.mergaite_mod_icon,false,false,R.drawable.mergaite1,R.raw.veikejai,0);
	  CollisionObj cob18 = new CollisionObj();
	  cob18.collisionobjid=63;
	  cob18.animationid=102;
	  ob18.co.add(cob18);
	  objects.add(ob18);
	  /*27*/objects.add(new ObjectHelper(R.drawable.moteris_mod_icon,false,false,R.drawable.moteris,R.raw.veikejai,0));
	  /*28*/objects.add(new ObjectHelper(R.drawable.tevas_mod_icon,false,false,R.drawable.tevas,R.raw.veikejai,0));
	  
	  //objects.add(new ObjectHelper(R.drawable.arbaletas_icon,false,false,R.drawable.arbaletas,R.raw.flamemagic));
	  /*29*/ObjectHelper ob=new ObjectHelper(R.drawable.arbaletas_icon,false,false,R.drawable.arbaletas,R.raw.zaislai,0);
	  CollisionObj cob = new CollisionObj();
	  cob.collisionobjid=4;
	  cob.animationid=91;
	  ob.co.add(cob);
	  objects.add(ob);
	  /*30*/objects.add(new ObjectHelper(R.drawable.dambleris_icon,false,false,R.drawable.dambleris,R.raw.dambleris,0));
	  /*31*/objects.add(new ObjectHelper(R.drawable.ietis_icon,false,false,R.drawable.ietis,R.raw.zaislai,0));
	  //objects.add(new ObjectHelper(R.drawable.kalavijas_icon,false,false,R.drawable.kalavijas,R.raw.flamemagic));
	  /*32*/ObjectHelper ob201=new ObjectHelper(R.drawable.kalavijas_icon,false,false,R.drawable.kalavijas,R.raw.zaislai,0);
	  CollisionObj cob201 = new CollisionObj();
	  cob201.collisionobjid=3;
	  cob201.animationid=103;
	  ob201.co.add(cob201);
	  objects.add(ob201);
	  /*33*/objects.add(new ObjectHelper(R.drawable.kamuolys_icon,false,false,R.drawable.kamuolys,R.raw.zaislai,0));
	  /*34*/objects.add(new ObjectHelper(R.drawable.kegliai_icon,false,false,R.drawable.kegliai,R.raw.kegliai_1,0));
	  //objects.add(new ObjectHelper(R.drawable.lankas_icon,false,false,R.drawable.lankas,R.raw.flamemagic));
	  /*35*/ObjectHelper ob1=new ObjectHelper(R.drawable.lankas_icon,false,false,R.drawable.lankas,R.raw.zaislai,0);
	  CollisionObj cob1 = new CollisionObj();
	  cob1.collisionobjid=3;
	  cob1.animationid=93;
	  ob1.co.add(cob1);
	  objects.add(ob1);
	 // objects.add(new ObjectHelper(R.drawable.leles_icon,false,false,R.drawable.leles,R.raw.flamemagic));
	  /*36*/ObjectHelper ob8=new ObjectHelper(R.drawable.leles_icon,false,false,R.drawable.leles,R.raw.zaislai,0);
	  objects.add(ob8);
	  
	  //objects.add(new ObjectHelper(R.drawable.peilis_icon,false,false,R.drawable.peilis,R.raw.flamemagic));
	  /*37*/ObjectHelper ob2=new ObjectHelper(R.drawable.peilis_icon,false,false,R.drawable.peilis,R.raw.zaislai,0);
	  CollisionObj cob2 = new CollisionObj();
	  cob2.collisionobjid=8;
	  cob2.animationid=94;
	  ob2.co.add(cob2);
	  objects.add(ob2);
	  /*38*/objects.add(new ObjectHelper(R.drawable.rutuliukai_icon,false,false,R.drawable.rutuliukai,R.raw.rutuliukai_01,0));
	  //objects.add(new ObjectHelper(R.drawable.sachmatai_icon,false,false,R.drawable.sachmatai,R.raw.flamemagic));
	  /*39*/ObjectHelper ob3=new ObjectHelper(R.drawable.sachmatai_icon,false,false,R.drawable.sachmatai,R.raw.sachmatai,0);
	  CollisionObj cob3 = new CollisionObj();
	  cob3.collisionobjid=9;
	  cob3.animationid=92;
	  ob3.co.add(cob3);
	  CollisionObj cob303 = new CollisionObj();
	  cob303.collisionobjid=10;
	  cob303.animationid=93;
	  ob3.co.add(cob303);
	  objects.add(ob3);
	  /*40*/objects.add(new ObjectHelper(R.drawable.sukutis_icon,false,false,R.drawable.sukutis_dv,R.raw.zaislai,0));
	  /*41*/objects.add(new ObjectHelper(R.drawable.tosine_valtele_icon,false,false,R.drawable.tos_valtele,R.raw.zaislai,0));
	  //objects.add(new ObjectHelper(R.drawable.uzines_icon,false,false,R.drawable.uzines,R.raw.uzine));
	  /*42*/ObjectHelper ob204=new ObjectHelper(R.drawable.uzines_icon,false,false,R.drawable.uzines,R.raw.uzine_pakeltas_daznis,0);
	  CollisionObj cob204 = new CollisionObj();
	  cob204.collisionobjid=16;
	  cob204.animationid=97;
	  ob204.co.add(cob204);
	  objects.add(ob204);
	  /*43*/objects.add(new ObjectHelper(R.drawable.vezimukas_icon,false,false,R.drawable.vezimukas,R.raw.zaislai,0));
	  /*44*/objects.add(new ObjectHelper(R.drawable.vilkelis_icon,true,true,R.drawable.anim_arch_vilkelis_200,1400,200,4,7,R.raw.zaislai,0));
	  
	  //objects.add(new ObjectHelper(R.drawable.kamuolys_dvaro_icon,false,false,R.drawable.kamuolys_dvaro,R.raw.flamemagic));
	  /*45*/ObjectHelper ob5=new ObjectHelper(R.drawable.kamuolys_dvaro_icon,false,false,R.drawable.kamuolys_dvaro,R.raw.zaislai,0);
	  CollisionObj cob205 = new CollisionObj();
	  cob205.collisionobjid=47;
	  cob205.animationid=99;
	  ob5.co.add(cob205);
	  CollisionObj cob5 = new CollisionObj();
	  cob5.collisionobjid=13;
	  cob5.animationid=98;
	  ob5.co.add(cob5);
	  objects.add(ob5);
	  /*46*/objects.add(new ObjectHelper(R.drawable.kardas_dvaro_icon,false,false,R.drawable.kardas_dvaro,R.raw.zaislai,0));
	  //objects.add(new ObjectHelper(R.drawable.kegliai_dvaro_icon,false,false,R.drawable.kegliai_dvaro,R.raw.flamemagic));
	  /*47*/ObjectHelper ob6=new ObjectHelper(R.drawable.kegliai_dvaro_icon,false,false,R.drawable.kegliai_dvaro,R.raw.kegliai_2,0);
	  CollisionObj cob206 = new CollisionObj();
	  cob206.collisionobjid=45;
	  cob206.animationid=99;
	  ob6.co.add(cob206);
	  CollisionObj cob6 = new CollisionObj();
	  cob6.collisionobjid=13;
	  cob6.animationid=99;
	  ob6.co.add(cob6);
	  objects.add(ob6);
	  /*48*/objects.add(new ObjectHelper(R.drawable.lankas_dvaro_icon,false,false,R.drawable.lankas_dvaro,R.raw.zaislai,0));
	  //objects.add(new ObjectHelper(R.drawable.lele_dvaro_icon,false,false,R.drawable.lele_dvaro,R.raw.flamemagic));
	  /*49*/ObjectHelper ob208=new ObjectHelper(R.drawable.lele_dvaro_icon,false,false,R.drawable.lele_dvaro,R.raw.zaislai,0);
	  CollisionObj cob208 = new CollisionObj();
	  cob208.collisionobjid=53;
	  cob208.animationid=96;
	  ob208.co.add(cob208);
	  objects.add(ob208);
	  //objects.add(new ObjectHelper(R.drawable.ragatke_dvaro_icon,false,false,R.drawable.ragatke_dvaro,R.raw.flamemagic));
	  /*50*/ObjectHelper ob308=new ObjectHelper(R.drawable.ragatke_dvaro_icon,false,false,R.drawable.ragatke_dvaro,R.raw.zaislai,0);
	  CollisionObj cob308 = new CollisionObj();
	  cob308.collisionobjid=13;
	  cob308.animationid=104;
	  ob308.co.add(cob308);
	  objects.add(ob308);
	  /*51*/objects.add(new ObjectHelper(R.drawable.sautuvas_dvaro_icon,false,false,R.drawable.sautuvas_dvaro,R.raw.sautuvas,0));
	  /*52*/objects.add(new ObjectHelper(R.drawable.svilpukas_dvaro_icon,false,false,R.drawable.svilpukas_dvaro,R.raw.svilpukas_2,0));
	  //objects.add(new ObjectHelper(R.drawable.uzine_dvaro_icon,false,false,R.drawable.uzine_dvaro,R.raw.flamemagic));
	  //ObjectHelper ob4=new ObjectHelper(R.drawable.uzine_dvaro_icon,false,false,R.drawable.uzine_dv,R.raw.uzine_pakeltas_daznis,0);
	 // CollisionObj cob4 = new CollisionObj();
	 // cob4.collisionobjid=16;
	 // cob4.animationid=97;
	 // ob4.co.add(cob4);
	  //objects.add(ob4);
	  //objects.add(new ObjectHelper(R.drawable.vezimukas_dvaro_icon,false,false,R.drawable.vezimukas_davro,R.raw.flamemagic));
	  /*53*/ObjectHelper ob211=new ObjectHelper(R.drawable.vezimukas_dvaro_icon,false,false,R.drawable.vezimukas_dv,R.raw.zaislai,0);
	  CollisionObj cob211 = new CollisionObj();
	  cob211.collisionobjid=49;
	  cob211.animationid=96;
	  ob211.co.add(cob211);
	  objects.add(ob211);
	  /*54*/objects.add(new ObjectHelper(R.drawable.valtele_dvaro_icon,false,false,R.drawable.valtele_dvaro,R.raw.zaislai,0));
	 /*55*/objects.add(new ObjectHelper(R.drawable.vilkelis_dvaro_icon,false,false,R.drawable.vilkelis_dv,R.raw.zaislai,0));
	 /*56*/ ObjectHelper ob504=new ObjectHelper(R.drawable.uzines_icon,false,false,R.drawable.uzines,R.raw.uzine_pakeltas_daznis,0);
	  CollisionObj cob504 = new CollisionObj();
	  cob504.collisionobjid=16;
	  cob504.animationid=97;
	  ob504.co.add(cob504);
	  objects.add(ob504);
	  
	  /*57*/objects.add(new ObjectHelper(R.drawable.automatas_mod_icon,false,false,R.drawable.automatas_mod,R.raw.automatas,0));
	  /*58*/objects.add(new ObjectHelper(R.drawable.burlaivis_mod_icon,false,false,R.drawable.burlaivis,R.raw.zaislai,0));
	  /*59*/objects.add(new ObjectHelper(R.drawable.kamuolys_mod_icon,false,false,R.drawable.kamuolys_mod,R.raw.zaislai,0));
	  /*60*/objects.add(new ObjectHelper(R.drawable.kardas_mod_icon,false,false,R.drawable.kardas,R.raw.zaislai,0));
	  /*61*/objects.add(new ObjectHelper(R.drawable.kegliai_mod_icon,false,false,R.drawable.kegliai_mod,R.raw.zaislai,0));
	  /*62*/objects.add(new ObjectHelper(R.drawable.lego_mod_icon,false,false,R.drawable.lego,R.raw.zaislai,0));
	  //objects.add(new ObjectHelper(R.drawable.lele_mod_icon,false,false,R.drawable.lele,R.raw.flamemagic));
	  /*63*/ObjectHelper ob7=new ObjectHelper(R.drawable.lele_mod_icon,false,false,R.drawable.lele_mod,R.raw.zaislai,0);
	  CollisionObj cob7 = new CollisionObj();
	  cob7.collisionobjid=26;
	  cob7.animationid=102;
	  ob7.co.add(cob7);
	  objects.add(ob7);
	  /*64*/objects.add(new ObjectHelper(R.drawable.masina_mod_icon,true,true,R.drawable.anim_masinele_new_700_resized,3111,110,4,8,R.raw.masina_nauja,0));
	  //objects.add(new ObjectHelper(R.drawable.vezimelis_mod_icon,false,false,R.drawable.vezimelis,R.raw.flamemagic));
	  /*65*/ObjectHelper ob11=new ObjectHelper(R.drawable.vezimelis_mod_icon,false,false,R.drawable.vezimelis,R.raw.vezimas,0);
	  objects.add(ob11);
	  /*66*/objects.add(new ObjectHelper(R.drawable.vilkelis_mod_icon,false,false,R.drawable.vilkelis_modern,R.raw.zaislai,0));  	  
	  /*67*/objects.add(new ObjectHelper(R.drawable.paukstis_icon,false,false,R.drawable.paukstis,R.raw.paukstis,0));
	  /*68*/objects.add(new ObjectHelper(R.drawable.vista_icon,false,false,R.drawable.vista,R.raw.vista,0));
	  /*69*/objects.add(new ObjectHelper(R.drawable.kate_icon,false,false,R.drawable.kate,R.raw.kate,0));
	  /*70*/objects.add(new ObjectHelper(R.drawable.suo_icon,false,false,R.drawable.suo,R.raw.suo,0));
	  /*71*/objects.add(new ObjectHelper(R.drawable.zirgas_icon,false,false,R.drawable.zirgas,R.raw.zirgas,0));
	  /*72*/objects.add(new ObjectHelper(R.drawable.karve_icon,false,false,R.drawable.karve,R.raw.karve,0));
	  /*73*/objects.add(new ObjectHelper(R.drawable.avis_icon,false,false,R.drawable.avis,R.raw.avis,0));
	  /*74*/objects.add(new ObjectHelper(R.drawable.briedis_icon,false,false,R.drawable.briedis,R.raw.gamtos_objektai,0));
	  /*75*/objects.add(new ObjectHelper(R.drawable.stumbras_icon,false,false,R.drawable.stumbras,R.raw.gamtos_objektai,0));
	  /*76*/objects.add(new ObjectHelper(R.drawable.pele_icon,false,false,R.drawable.pele,R.raw.gamtos_objektai,0));
	 
	  /*77*/objects.add(new ObjectHelper(R.drawable.daugiabutis_icon,false,false,R.drawable.daugiabutis,R.raw.aplinkos_objektai,0));
	  /*78*/objects.add(new ObjectHelper(R.drawable.debesis_icon,false,false,R.drawable.debesis,R.raw.aplinkos_objektai,0));
	  /*79*/objects.add(new ObjectHelper(R.drawable.dvarelis_icon,false,false,R.drawable.dvarelis,R.raw.aplinkos_objektai,0));
	  /*80*/objects.add(new ObjectHelper(R.drawable.dviratis_icon,false,false,R.drawable.dviratis,R.raw.dviratis,0));
	  /*81*/objects.add(new ObjectHelper(R.drawable.kompas_icon,false,false,R.drawable.kompas,R.raw.kompiuteris,0));
	  /*82*/objects.add(new ObjectHelper(R.drawable.langas_icon,false,false,R.drawable.langas,R.raw.aplinkos_objektai,0));
	  /*83*/objects.add(new ObjectHelper(R.drawable.masina_apl_icon,false,false,R.drawable.masina_apl,R.raw.masina,0));
	  /*84*/objects.add(new ObjectHelper(R.drawable.medis_icon,false,false,R.drawable.medis,R.raw.aplinkos_objektai,0));
	  /*85*/objects.add(new ObjectHelper(R.drawable.pilis_icon,false,false,R.drawable.pilis,R.raw.aplinkos_objektai,0));
	  /*86*/objects.add(new ObjectHelper(R.drawable.saule_icon,false,false,R.drawable.saule,R.raw.aplinkos_objektai,0));
	  /*87*/objects.add(new ObjectHelper(R.drawable.sulinys_icon,false,false,R.drawable.sulinys,R.raw.aplinkos_objektai,0));
	  /*88*/objects.add(new ObjectHelper(R.drawable.taikinys_icon,false,false,R.drawable.taikinys,R.raw.taikinys,0));
	  /*89*/objects.add(new ObjectHelper(R.drawable.telikas_icon,false,false,R.drawable.telikas,R.raw.aplinkos_objektai,0));
	  /*90*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,false,false,R.drawable.vezimas,R.raw.vezimas,0));
	  
	  /*91*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,false,R.drawable.anm_archeologija_berniukas_svecias_arbaletas_800,5600,500,3,7,R.raw.strele_paleidziama,1));
	  /*92*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,true,R.drawable.anim_archeologija_riteriai_shachmatai_700,5600,400,3,8,R.raw.sachmatai,0));
	  /*93*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,false,R.drawable.anim_archeologija_riterio_sunus_lankas_700,4200,500,3,6,R.raw.strele_paleidziama,2));
	  /*94*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,true,R.drawable.anim_archeologija_tevas_drozineja_400,1600,400,3,4,R.raw.veikejai,5));
	  /*95*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,true,R.drawable.anim_arch_vilkelis_200,1400,200,4,7,R.raw.zaislai,0));
	  /*96*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,true,R.drawable.anim_etnografija_lele_vezimas_300,1200,200,3,4,R.raw.vezimas,0));
	  /*97*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,true,R.drawable.anim_etnografija_mergaite_uzine_300,2700,400,3,9,R.raw.uzine_pakeltas_daznis,0));
	  /*98*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,false,R.drawable.anim_etnografija_ukininko_sunus_kamuolys_700,4200,400,3,6,R.raw.kamuolys_spiria,3));
	  /*99*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,false,R.drawable.anim_etnografija_ukininko_sunus_kegliai_900,5400,400,6,6,R.raw.kegliai3,4));
	  /*100*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,false,R.drawable.anim_modernus_dzedajus_400,3600,400,3,9,R.raw.dzedajus,0));
	  /*101*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,true,R.drawable.anim_modernus_masinele_500,5600,198,3,8,R.raw.masina,0));
	  /*102*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,true,true,R.drawable.anim_modernus_mergaite_lele_300,1200,300,3,4,R.raw.veikejai,0));
	  /*103*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,false,false,R.drawable.berniukas_riteris,180,313,1,1,R.raw.zaislai,0));
	  /*104*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,false,false,R.drawable.berniukas_ragatke_davro,190,293,1,1,R.raw.zaislai,0));
	  /*105*/objects.add(new ObjectHelper(R.drawable.vezimas_icon,false,false,R.drawable.berniukas,181,296,1,1,R.raw.kamuolys_nukrenta,0));
	  // SimpleActor imoobj = new SimpleActor(BitmapFactory.decodeResource(getResources(), R.drawable.dots), 50, 50);
	 // imo.add(imoobj);
	  Bitmap bm=BitmapFactory.decodeResource(getResources(), R.drawable.zm);//BitmapFactory.decodeResource(getResources(), objects.get(resint).image);
      bm=Bitmap.createScaledBitmap(bm, 1280, 800, true);
	  fonas = new SimpleActor(bm, 640, 400,0,R.raw.aplinkos,0);
	  imo.clear();
	 /* AnimatedActor elaine = new AnimatedActor(BitmapFactory.decodeResource(getResources(), R.drawable.walk_elaine)
			  	                , 10, 50    // initial position
			  	                , 30, 47    // width and height of sprite
			  	                , 5, 5);    // FPS and number of frames in the animation
	  //elaine.setAnimationLooped(true);
	  imo.add(elaine);*/

	  // make the GamePanel focusable so it can handle events
	  thread = new MainThread(getHolder(), this);
	  
      
      //this.
	 // thread.setRunning(true);
     // thread.start();
	  setFocusable(true);
	 }
	 
	 public static int calculateInSampleSize(
	            BitmapFactory.Options options, int reqWidth, int reqHeight) {
	    // Raw height and width of image
	    final int height = options.outHeight;
	    final int width = options.outWidth;
	    int inSampleSize = 1;

	    if (height > reqHeight || width > reqWidth) {
	        if (width > height) {
	            inSampleSize = Math.round((float)height / (float)reqHeight);
	        } else {
	            inSampleSize = Math.round((float)width / (float)reqWidth);
	        }
	    }
	    return inSampleSize;
	}
	 
	 public void changeBack(int resint){
		 //final BitmapFactory.Options options = new BitmapFactory.Options();
		 //options.inJustDecodeBounds = true;
		 //BitmapFactory.decodeResource(getResources(), objects.get(resint).image, options);
		 //options.inSampleSize = calculateInSampleSize(options, 1280, 800); //options.inSampleSize = calculateInSampleSize(options, 640, 400);
		 //options.inJustDecodeBounds = false;
		 Bitmap bm=BitmapFactory.decodeResource(getResources(), objects.get(resint).image);//, options);//BitmapFactory.decodeResource(getResources(), objects.get(resint).image);
		 bm=Bitmap.createScaledBitmap(bm, 1280, 800, true);
		 fonas.bitmap=bm;//=new SimpleActor(bm, 600, 400,0);
	 }
	 
	 public void update() {
		     for(int i=0;i<imo.size();i++){
		    	 if (imo.get(i).isAnimated()){
			       ((AnimatedActor)imo.get(i)).update(System.currentTimeMillis());
		    	 }
		     }
		}

	
	 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	 }
	 
	
	 public void surfaceCreated(SurfaceHolder holder) {
		 thread.setRunning(true);
         thread.start();
	 }
	 
	 
	 public void surfaceDestroyed(SurfaceHolder holder) {
		 boolean retry = true;
		 	  while (retry) {
		    try {
		 	    thread.join();
		 	    retry = false;
		 	   } catch (InterruptedException e) {
		 	    // try again shutting down the thread
		 	   }
		 	  }
	 }
	 
	 private void init() {
		    maxZoom = 4;
		    minZoom = 0.25f;
		    height = imobj.getBitmapHeight()+20;
		    width = imobj.getBitmapWidth()+20;
		    initheight = imobj.getBitmapHeight();
		    initwidth = imobj.getBitmapWidth();
		    initbitmap=imobj.bitmap;
		    viewRect = new RectF(0, 0, imobj.bitmap.getWidth()+20, imobj.bitmap.getHeight()+20);
		}

	 @Override
	 public boolean onTouchEvent(MotionEvent event) {
		/* if (event.getAction() == MotionEvent.ACTION_DOWN) {
			 imobj=getSelectedObject(event);
			 init();
			 if (imobj!=null){
				 
				  for(int i=0;i<imo.size();i++){
					  imo.get(i).setTouched(false);
				  }
			      imobj.handleActionDown((int)event.getX(), (int)event.getY());
			     // ZaisluMuziejusActivity.onItemLongClickInGamePael(imobj);
			 }else{
			 	 // if (event.getY() > getHeight() - 50) {
			 	   // thread.setRunning(false);
			 	   // ((Activity)getContext()).finish();
			 	   //} else {
			 		  if (event.getDownTime()> 4000){
			 			 menuCtrl();
			 		  }
			 	   // }
			 }
		} 
		  if (event.getAction() == MotionEvent.ACTION_MOVE) {
			 	   // the gestures
			  if (imobj!=null){
			 	   if (imobj.isTouched()) {
			 	    // the droid was picked up and is being dragged
			 		  imobj.setX((int)event.getX());
			 		 imobj.setY((int)event.getY());
			 	   }
			  }
		  } 
		  if (event.getAction() == MotionEvent.ACTION_UP) {
			 	   // touch was released
			      if (imobj!=null){
			 	   if (imobj.isTouched()) {
			 		  //imobj.setTouched(false);
			 		  //jei vieno ciklo animacija paleisti is naujo
			 		  if (imobj.isAnimated() && !imobj.isAnimationLooped()){
			 			 ((AnimatedActor)imobj).setCurrentFrame(0);
			 		  }
			 	   }
			      }
			}*/
		  
		  
		// Handle touch events here...
		   switch (event.getAction() & MotionEvent.ACTION_MASK) {
		    case MotionEvent.ACTION_DOWN:
		        savedMatrix.set(matrix);
		        start.set(event.getX(), event.getY());
		        Log.d(TAG, "mode=DRAG");
		        mode = DRAG;
		        imobj=getSelectedObject(event);
		        
		        long thisTime = System.currentTimeMillis();
		        if (thisTime - lastTouchTime < 250) {
		        	if (imobj!=null && !imobj.isAnimated()){
		        		Bitmap bmp=imobj.getBitmap();
		        		Matrix matrix = new Matrix(); 
		        		matrix.preScale(-1.0f, 1.0f); 
		        		Bitmap mirroredBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);
                        imobj.setBitmap(mirroredBitmap);
		        	}
		           lastTouchTime = -1;
                   break;
		        } else {
		           // too slow
		           lastTouchTime = thisTime;
		        }
		        
			 if (imobj!=null){
				 //if (imobj.animated){return false;};
				 init();
				  for(int i=0;i<imo.size();i++){
					  imo.get(i).setTouched(false);
				  }
			      imobj.handleActionDown((int)event.getX(), (int)event.getY());
			      if (deleteStatus){
			    	  for(int i=0;i<imo.size();i++){
						   if (MainGamePanel.imo.get(i).isTouched()){
							   MainGamePanel.imo.remove(i);
						   }
		        	}
			      }else{
			    	  for(int i=0;i<imo.size();i++){
						   if (MainGamePanel.imo.get(i).isTouched()){
							   Actor tmp=MainGamePanel.imo.get(i);
							   imo.set(i, imo.get(imo.size()-1));
							   imo.set(imo.size()-1,tmp);
						   }
		        	} 
			      }
			     // ZaisluMuziejusActivity.onItemLongClickInGamePael(imobj);
			 }else{
			 	 // if (event.getY() > getHeight() - 50) {
			 	   // thread.setRunning(false);
			 	   // ((Activity)getContext()).finish();
			 	   //} else {
			 		  if (event.getDownTime()> 4000){
			 			 menuCtrl();
			 		  }
			 	   // }
			 }
		        break;
		    case MotionEvent.ACTION_POINTER_DOWN:
		        oldDist = spacing(event);
		        Log.d(TAG, "oldDist=" + oldDist);
		        if (oldDist > 10f) {
		            savedMatrix.set(matrix);
		            midPoint(mid, event);
		            mode = ZOOM;
		            Log.d(TAG, "mode=ZOOM");
		        }
		        break;
		    case MotionEvent.ACTION_UP:
		    if (imobj!=null){
			 	   if (imobj.isTouched()) {
			 		  //imobj.setTouched(false);
			 		  //jei vieno ciklo animacija paleisti is naujo
			 		  if (imobj.isAnimated() && !imobj.isAnimationLooped()){
			 			 ((AnimatedActor)imobj).setCurrentFrame(0);
			 		  }
			 		 if (imobj.isAnimationLooped()){
			 		   playSound(imobj.soundID,5);
			 		 }else{playSound(imobj.soundID,0);}
			 	   }
			      }
		    case MotionEvent.ACTION_POINTER_UP:
		        mode = NONE;
		        Log.d(TAG, "mode=NONE");
		        break;
		    case MotionEvent.ACTION_MOVE:
		        if (mode == DRAW){ onTouchEvent(event);}
		        if (mode == DRAG) {
		                if (imobj!=null){
			 	   if (imobj.isTouched()) {
			 	    // the droid was picked up and is being dragged
			 		  switch (imobj.animationOrientation){
	    	            case 0:imobj.setX((int)event.getX());;break;
	    	            case 1:imobj.setX((int)event.getX()+200);;break;
	    	            } 
			 		  imobj.setX((int)event.getX());
			 		 imobj.setY((int)event.getY());
			 	   }
			  }    
		        } 
		     else if (mode == ZOOM) {
		         float newDist = spacing(event);
		         Log.d(TAG, "newDist=" + newDist);
		         if (newDist > 10f) {
		             matrix.set(savedMatrix);
		             scale = newDist / oldDist;
		             matrix.getValues(matrixValues);
		             float currentScale = matrixValues[Matrix.MSCALE_X];
		             // limit zoom
		             if (scale * currentScale > maxZoom) {
		                 scale = maxZoom / currentScale; 
		                }else if(scale * currentScale < minZoom){
		                    scale = minZoom / currentScale; 
		                 }
		             matrix.postScale(scale, scale, mid.x, mid.y);
		             
		             //String msg="Zoom mast:"+scale;
		             //ZaisluMuziejusActivity.pranesimas(msg);
		            // imobj.bitmap=Bitmap.createBitmap(imobj.bitmap, imobj.getX(), imobj.getY(), imobj.getBitmapWidth(), imobj.getBitmapHeight(), matrix, false);
		            // if (scale>2){scale=2;} else if (scale<0.5){scale=0.5f;}
		            // if (mode==ZOOM){
		            // if((20<initwidth)&&(initwidth<400)){
		            	try{
		            		if (imobj.isAnimated()==false){
		 		    	      Bitmap bm=Bitmap.createScaledBitmap(initbitmap, (int)(initwidth*scale), (int)(initheight*scale), false);
		 	                  imobj.setBitmap(bm);  
		            		}
		            	}catch(Exception ex){}
		           //  }
		 		    	//}
		         }
		     }
		     break;
		    }
		    
		  
			 	  return true;
	 }
	 
	 private Actor getSelectedObject(MotionEvent event) {
		// TODO Auto-generated method stub
		 Actor rez=null;
	//	Log.d(TAG,"event X:"+event.getRawX()+" Y:"+event.getRawY());
		for(int i=0;i<imo.size();i++){
			Actor ob=(Actor)imo.get(i);
			if (ob.animated){
				//CharSequence text = ob.getX()+":"+ob.getY()+">>>>>"+event.getRawX()+":"+event.getRawY()+">>>>>"+ob.getFrameWidth()+"---"+ob.getFrameHeight();
				//int duration = Toast.LENGTH_SHORT;
				//Toast toast = Toast.makeText(ctx, text, duration);
				//toast.show();
				
				if ((((ob.getX()-ob.getFrameWidth()/2)<event.getRawX()) && (event.getRawX()<ob.getX()-ob.getFrameWidth()/2+ob.getFrameWidth())) && ((ob.getY()-ob.getFrameHeight()/2<event.getRawY()) && (event.getRawY()<ob.getY()-ob.getFrameHeight()/2+ob.getFrameHeight()))){
				       rez=ob;
					}
			}else if ((((ob.getX()-ob.getBitmap().getWidth()/2)<event.getRawX()) && (event.getRawX()<ob.getX()-ob.getBitmap().getWidth()/2+ob.getBitmap().getWidth())) && ((ob.getY()-ob.getBitmap().getHeight()/2<event.getRawY()) && (event.getRawY()<ob.getY()-ob.getBitmap().getHeight()/2+ob.getBitmap().getHeight()))){
				       rez=ob;
					}		
	     }
		return rez;
	}

	public boolean onTouchEvent(MotionEvent event,View arg,int ix) {
		     if (event.getAction() == MotionEvent.ACTION_UP) {
		    	 
		    	 if (ix<3){
		    		 changeBack(ix);
		    	 }else{
		    	 int colid[]={-1,-1};
		    	 colid=getColisionId((int)event.getX(),(int)event.getY(),ix);
		    	 if ((colid[0]>=0) && (colid[1]>=0)){
		    		  imo.remove(colid[0]);
		        	  if(objects.get(colid[1]).image==R.drawable.anim_archeologija_riteriai_shachmatai_700){
		    		  for (int x=0;x<imo.size();x++){
		    			 if ((imo.get(x).objid==9) || (imo.get(x).objid==10)){
		    				 imo.remove(x);
		    			 }
		    		  }
		    		  }
		    		 // imo.remove(colid[1]);
		    		  AnimatedActor imobj1 = new AnimatedActor(BitmapFactory.decodeResource(getResources(), objects.get(colid[1]).image), (int)event.getX(), (int)event.getY(), objects.get(colid[1]).widthSprite, objects.get(colid[1]).heightSprite, objects.get(colid[1]).fpsSprite, objects.get(colid[1]).frameNumberSprite,colid[1],objects.get(colid[1]).soundid,objects.get(colid[1]).animationOrientation);
		    		  imobj1.setAnimationLooped(objects.get(colid[1]).animatedlooped);
		    		  imobj1.setTouched(false);
					  imo.add(imobj1);
		    	 }else{
		    	  if (objects.get(ix).isanimated==true){
		    		  AnimatedActor imobj1 = new AnimatedActor(BitmapFactory.decodeResource(getResources(), objects.get(ix).image), (int)event.getX(), (int)event.getY(), objects.get(ix).widthSprite, objects.get(ix).heightSprite, objects.get(ix).fpsSprite, objects.get(ix).frameNumberSprite,ix,objects.get(ix).soundid,objects.get(ix).animationOrientation);
		    		  imobj1.setAnimationLooped(objects.get(ix).animatedlooped);
		    		  imobj1.setTouched(false);
					  imo.add(imobj1);  
		    	  }else{
		    	   SimpleActor imobj1 = new SimpleActor(BitmapFactory.decodeResource(getResources(), objects.get(ix).image), (int)event.getX(), (int)event.getY(),ix,objects.get(ix).soundid,objects.get(ix).animationOrientation);
		    	   imobj1.setTouched(false);
				   imo.add(imobj1);
		    	  }
		    	 }
		    	 }
		    	   
			 }
		    

		     return true;
		 }
	
	/*boolean imageIsOpaque(int touchX, int touchY) {
	    ArrayList<Rect> rectsOfOpaqueness; // You will need to define these ...

	    boolean isOpaque = false;
	    for (int i=0; i<rectsOfOpaqueness.size() && !isOpaque; i++) {
	    if (rectsOfOpaqueness.get(i).contains(touchX, touchY)) {
	        isOpaque = true;
	        }
	    }
	    return isOpaque;
	}*/
	 
	 private int[] getColisionId(int x,int y,int id) {
		// TODO Auto-generated method stub
		int rez[]={-1,-1};
		for(int i=0;i<imo.size();i++){
			int yy=imo.get(i).getY();
			int hg=imo.get(i).getBitmapHeight();
			int xx=imo.get(i).getX();
			int wd=imo.get(i).getBitmapWidth();
			int idd=imo.get(i).objid;
			if ((yy-hg/2<=y)&&(y<=yy+hg/2))
			if ((xx-wd/2<=x)&&(x<=xx+wd/2)){
				for(int j=0;j<objects.get(id).co.size();j++){
					int colid=objects.get(id).co.get(j).collisionobjid;
					if (colid==idd){
			        	rez[0]=i;
				        rez[1]=objects.get(id).co.get(j).animationid;
					}
				}
			}
		}
		return rez;
	}

	@Override
	 protected void onDraw(Canvas canvas) {
		// canvas.drawColor(Color.GREEN);
		// imobj.draw(canvas);
	 }
	 
	 public void render(Canvas canvas) {
			canvas.drawColor(Color.BLACK);
			fonas.draw(canvas);
			//Matrix refMatrix = new Matrix();
			//refMatrix.postScale(0.5f, 0.5f);
			//canvas.concat(refMatrix);
			 for(int i=0;i<imo.size();i++){
				   if (imo.get(i).isAnimated()){
					   ((AnimatedActor)imo.get(i)).draw(canvas);
				   }else{
					   ((SimpleActor)imo.get(i)).draw(canvas);
				   }
			     }
			// canvas.restore();
		}
	 
	 public static void menuCtrl(){
		 if (menuIsVisible==false){
	 		  ZaisluMuziejusActivity.callMenu();
	 		  menuIsVisible=true;
		    }else{
		    	ZaisluMuziejusActivity.hideMenu();
		    	menuIsVisible=false;
		    }
	 }
	 
	//*******************Determine the space between the first two fingers
	 private float spacing(MotionEvent event) {
	    float x = event.getX(0) - event.getX(1);
	    float y = event.getY(0) - event.getY(1);
	    return FloatMath.sqrt(x * x + y * y);
	 }

	 //************* Calculate the mid point of the first two fingers 
	 private void midPoint(PointF point, MotionEvent event) {
	    float x = event.getX(0) + event.getX(1);
	    float y = event.getY(0) + event.getY(1);
	    point.set(x / 2, y / 2);
	 }
	 
	 public void playSound(int soundid,int loop){
			if (soundid!=0){
				ZaisluMuziejusActivity.soundLoop	= loop;
				ZaisluMuziejusActivity.soundID = ZaisluMuziejusActivity.soundPool.load(ZaisluMuziejusActivity.ctxx, soundid, 1);
			}
		}
	  
}


